/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.elysian.translator;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 *
 * @author ydubey
 */
public class HTMLSpclCharsConverter
{

    private static Map<String, String> symbols = new HashMap<String, String>();

    static
    {
        symbols.put("&ndash;", "–");
        symbols.put("&mdash;", "—");
        symbols.put("&iexcl;", "¡");
        symbols.put("&iquest;", "¿");
        symbols.put("&quot;", "\"");
        symbols.put("&ldquo;", "“");
        symbols.put("&rdquo;", "”");
        symbols.put("&lsquo;", "‘");
        symbols.put("&rsquo;", "’");
        symbols.put("&laquo;", "«");
        symbols.put("&raquo;", "»");
        symbols.put("&nbsp;", " ");
        symbols.put("&amp;", "&");
        symbols.put("&cent;", "¢");
        symbols.put("&copy;", "©");
        symbols.put("&divide;", "÷");
        symbols.put("&gt;", ">");
        symbols.put("&lt;", "<");
        symbols.put("&micro;", "µ");
        symbols.put("&middot;", "·");
        symbols.put("&para;", "¶");
        symbols.put("&plusmn;", "±");
        symbols.put("&euro;", "€");
        symbols.put("&pound;", "£");
        symbols.put("&reg;", "®");
        symbols.put("&sect;", "§");
        symbols.put("&yen;", "¥");
        symbols.put("&aacute;", "á");
        symbols.put("&Aacute;", "Á");
        symbols.put("&agrave;", "à");
        symbols.put("&Agrave;", "À");
        symbols.put("&acirc;", "â");
        symbols.put("&Acirc;", "Â");
        symbols.put("&aring;", "å");
        symbols.put("&Aring;", "Å");
        symbols.put("&atilde;", "ã");
        symbols.put("&Atilde;", "Ã");
        symbols.put("&auml;", "ä");
        symbols.put("&Auml;", "Ä");
        symbols.put("&aelig;", "æ");
        symbols.put("&AElig;", "Æ");
        symbols.put("&ccedil;", "ç");
        symbols.put("&Ccedil;", "Ç");
        symbols.put("&eacute;", "é");
        symbols.put("&Eacute;", "É");
        symbols.put("&egrave;", "è");
        symbols.put("&Egrave;", "È");
        symbols.put("&ecirc;", "ê");
        symbols.put("&Ecirc;", "Ê");
        symbols.put("&euml;", "ë");
        symbols.put("&Euml;", "Ë");
        symbols.put("&iacute;", "í");
        symbols.put("&Iacute;", "Í");
        symbols.put("&igrave;", "ì");
        symbols.put("&Igrave;", "Ì");
        symbols.put("&icirc;", "î");
        symbols.put("&Icirc;", "Î");
        symbols.put("&iuml;", "ï");
        symbols.put("&Iuml;", "Ï");
        symbols.put("&ntilde;", "ñ");
        symbols.put("&Ntilde;", "Ñ");
        symbols.put("&oacute;", "ó");
        symbols.put("&Oacute;", "Ó");
        symbols.put("&ograve;", "ò");
        symbols.put("&Ograve;", "Ò");
        symbols.put("&ocirc;", "ô");
        symbols.put("&Ocirc;", "Ô");
        symbols.put("&oslash;", "ø");
        symbols.put("&Oslash;", "Ø");
        symbols.put("&otilde;", "õ");
        symbols.put("&Otilde;", "Õ");
        symbols.put("&ouml;", "ö");
        symbols.put("&Ouml;", "Ö");
        symbols.put("&szlig;", "ß");
        symbols.put("&uacute;", "ú");
        symbols.put("&Uacute;", "Ú");
        symbols.put("&ugrave;", "ù");
        symbols.put("&Ugrave;", "Ù");
        symbols.put("&ucirc;", "û");
        symbols.put("&Ucirc;", "Û");
        symbols.put("&uuml;", "ü");
        symbols.put("&Uuml;", "Ü");
        symbols.put("&yuml;", "ÿ");
    }
    private static Set<String> symbolKeys = symbols.keySet();

    public static String convert(String text)
    {
        SymbolInfo symbolInfo = null;
        do
        {
            symbolInfo = getSymbolInfo(text);
            if (symbolInfo != null)
            {
                text = text.replace(symbolInfo.symbolText, symbolInfo.symbol);
            }
        } while (symbolInfo != null);
        return text;
    }

    private static SymbolInfo getSymbolInfo(String text)
    {
        SymbolInfo symbolInfo = null;
        for (String symbolKey : symbolKeys)
        {
            if (text.indexOf(symbolKey) != -1)
            {
                symbolInfo = new SymbolInfo(symbolKey, symbols.get(symbolKey));
                return symbolInfo;
            }
        }
        for (String symbolKey : symbolKeys)
        {
            String symbolKeyMod = symbolKey.substring(0, symbolKey.length() - 1);
            int index = text.indexOf(symbolKeyMod);
            if (index != -1)
            {
                index += symbolKeyMod.length();
                boolean change = false;
                while (index < text.length() && text.charAt(index) == ' ' || text.charAt(index) == '\t')
                {
                    symbolKeyMod += text.charAt(index);
                    index++;
                }
                if (index < text.length())
                {
                    if (text.charAt(index) == ';')
                    {
                        symbolKeyMod += text.charAt(index);
                        change = true;
                    }
                }
                if (change)
                {
                    symbolInfo = new SymbolInfo(symbolKeyMod, symbols.get(symbolKey));
                    return symbolInfo;
                }
            }
        }
        return symbolInfo;
    }

    static class SymbolInfo
    {

        public String symbolText;
        public String symbol;

        private SymbolInfo(String symbolText, String symbol)
        {
            this.symbolText = symbolText;
            this.symbol = symbol;
        }
    }
}
